<?php
   /*************************************************************************
    * File    : initmpay24_s.php
    * Version : $Id: initmpay24_s.php,v 1.22 2009/08/27 09:35:13 thomas Exp $
    * Author  : wolfgang.schaefer@mPAY24.com, thomas.langer@mPAY24.com
    *************************************************************************/

   include_once("SelectPayment.php");
   

   $etp_url       = "test";
   #$etp_url       = "www";
   $request       = new SelectPayment($etp_url);
   $billing_addr  = new Address();
   $style         = new Style();
   
   // if you do not want any items to be displayed, do not forget to remove the following class initializations
   $item_1        = new Item();
   #$item_2        = new Item();
   #item_n        = new Item();
   $tid           = "1234ABCabc";
   
   // MANDATORY
   $request->setMerchantId("91953");
   $request->setTid($tid);		// if there is no TID given, a random TID will be created
   
   // MANDATORY the PRICE to be authorised/billed. Please note that this should be read from a database, session information ...
   $request->setTotalPrice("9.50");				// MIND THE FORMAT: x.yz
   
   // OPTIONAL (xml-file)
   #$request->setUserField("userfield");
   #$request->setCssName("");						// do not change this, unless you are instructed by mPAY24 to do so
   $request->setLanguage("DE");
   #$request->setDescription("");
   
   // OPTIONAL Shipping Costs, Tax, ...
   #$request->addShippingCosts("Mindermengenzuschlag:,2.50");	// you can rename the field in the format you see to the left
   #$request->addShippingCosts("5.00");							// if no name is given, the default is used - MIND THE FORMAT: x.yz
   #$request->addTax("20,MwSt.,9.68");							// format: percentage,header,value - MIND THE FORMAT: x.yz
   $request->addTax("10,0.86"); 								// format: percentage,value - MIND THE FORMAT: x.yz
   #$request->addTax("9.68");									// format: header,value - MIND THE FORMAT: x.yz
   #$request->addTax("22.92");									// format: value - MIND THE FORMAT: x.yz

   // OPTIONAL, default: EUR
   // PLEASE REFER TO DOCUMENTATION FOR A COMPLETE LIST OF AVAILABLE CURRENCIES
   #$request->setCurrency("EUR");								// MIND THE FORMAT: three digit ISO currency code!
   
   // OPTIONAL - only available with mPAY24 proSAFE
   #$request->addCustomer("1234,true");

   // OPTIONAL - USE WITH CARE!
   /* Set Payment Types that should be offered to customer
    * If you uncomment the following block, all available payment types for the Merchant ID will be offered to the 
    * customer
    * If you specify only one of the following, the customer will be directed directly to the accordant page
    * If no Payment Type specified in this place is available for the Merchant ID, an error message will be displayed!
   */
   $request->setPaymentEnab("0");               // OPTIONAL - enable the following payment types for display (1 - 
   												// default) or disable (0)
   #$request->addPaymentType("CC");              // all available Creditcard Brands
   #$request->addPaymentType("CC,AMEX");         // American Express
   #$request->addPaymentType("CC,DINERS");       // Diners Club
   #$request->addPaymentType("CC,JCB");          // JCB
   #$request->addPaymentType("CC,MASTERCARD");   // MasterCard/MasterCard SecureCode
   #$request->addPaymentType("CC,VISA");         // VISA/Verified by VISA
   $request->addPaymentType("ELV");             // Elektronisches Lastschriftverfahren (alle verf�gbaren)
   $request->addPaymentType("ELV,ATOS");		// Elektronisches Lastschriftverfahren (f�r deutsche Bankkunden �ber 
   												// Atos Origin)
   $request->addPaymentType("ELV,HOBEX-AT");    // Elektronisches Lastschriftverfahren (f�r �sterreichische Bankkunden 
   												// �ber hobex)
   $request->addPaymentType("ELV,HOBEX-DE");    // Elektronisches Lastschriftverfahren (f�r deutsche Bankkunden hobex)
   $request->addPaymentType("ELV,HOBEX-NL");    // Elektronisches Lastschriftverfahren (f�r niederl�ndische Bankkunden 
   												// hobex)
   $request->addPaymentType("EPS");             // all available EPS Brands
   $request->addPaymentType("EPS,ARZ");         // Bankhaus Carl Sp�ngler & Co. AG, Bank f�r �rzte und Freie Berufe, 
   												// Die �sterreichischen Volksbanken, Immo-Bank, G�rtnerbank, Hypo 
   												// Alpe-Adria-Bank AG, HYPO Alpe-Adria-Bank International AG, Hypo 
   												// Tirol Bank AG, Investkreditbank AG, Nieder�sterreichische 
   												// Landesbank-Hypothekenbank AG, Vorarlberger Landes- und 
   												// Hypothekenbank AG
   $request->addPaymentType("EPS,BA");          // Bank Austria Creditanstalt AG
   $request->addPaymentType("EPS,BAWAG");       // BAWAG P.S.K. Gruppe
   $request->addPaymentType("EPS,ERSTE");       // Erste Bank
   $request->addPaymentType("EPS,HYPO");        // HYPO Ober�sterreich
   $request->addPaymentType("EPS,RZB");         // Raiffeisen Bankengruppe
   $request->addPaymentType("GIROPAY");			// giropay
   $request->addPaymentType("MAESTRO");         // Maestro SecureCode
   $request->addPaymentType("MIA");             // all available "Mobile Interface Austria" Brands
   $request->addPaymentType("MIA,ONE");         // ONE
   $request->addPaymentType("MIA,T-MOBILE");    // T-Mobile
   $request->addPaymentType("PB");              // paybox
   $request->addPaymentType("PSC");             // paysafecard
   $request->addPaymentType("QUICK");           // @Quick
 
   // OPTIONAL - set CSS Attributes for the payment window
   /* the following are only some entries of changes that can be done to the payment window. For a complete list, 
    * please refer to the schema at
    * https://www.mPAY24.com/schemas/MDXI/v3.0/MDXI.xsd 
   */
   $style->setLogoStyle("border: none;padding-bottom: 10px;margin: 10px auto;");
   $style->setPageHeaderStyle("text-align: left;");
   $style->setPageCaptionStyle("color:#FFFFFF;font-size:9pt;font-weight:bold;padding:4px 10px 1px;margin-right:5px;");
   $style->setPageStyle("");
   $style->setSCStyle("");
   $style->setSCHeader("");
   $style->setSCHeaderStyle("text-align:left;");
   $style->setSCCaptionStyle("color:#FFFFFF;font-size:14px;font-weight:bold;background-repeat:no-repeat;background-position:left top;margin-right:5px;padding: 2px 10px 1px;");
   $style->setSCNumberStyle("color:#333333;border:1px solid;");
   $style->setSCProductNrStyle("color:#333333;border:1px solid;");
   $style->setSCDescriptionStyle("color:#333333;border:1px solid;");
   $style->setSCPackageStyle("color:#333333;border:1px solid;");
   $style->setSCQuantityStyle("color:#333333;border:1px solid;");
   $style->setSCItemPriceStyle("color:#333333;border:1px solid;");
   $style->setSCPriceStyle("color:#333333;border:1px solid;");     
   $style->setSCItemEvenStyle("border:1px solid; padding-left:3px;padding-right:3px;");
   $style->setSCItemOddStyle("border:1px solid;padding-left:3px;padding-right:3px;");
   $style->setShippingCostsHStyle("text-align:right;padding-right: 3px;");
   $style->setShippingCostsStyle("text-align:right;padding-left:3px;padding-right: 3px;");
   $style->setTaxHStyle("text-align:right;padding-right:3px;");
   $style->setTaxStyle("text-align:right;padding-left:3px;padding-right:3px;");
   $style->setPRHeader("");
   $style->setPRHeaderStyle("text-align:right;padding-right: 3px;");
   $style->setPRStyle("border-top:2px solid;font-weight:bold;");
   $style->setWholePageStyle("margin-left: auto; margin-right: auto; width:600px;");   // Centered WMBI
   $style->setFooterStyle("margin-top: 2px;");
   $request->setStyle($style);
   
   // OPTIONAL - set BillingAddress
   $billing_addr->setFirstName("Max");
   $billing_addr->setLastName("Mustermann");
   $billing_addr->setStreet("Dorfstrasse");
   $billing_addr->setZip("1010");
   $billing_addr->setCity("Wien");
   $billing_addr->setCountry("Oesterreich");
   $billing_addr->setEmail("Support@mPAY24.com");
   $request->setBillingAddress($billing_addr);
   
   // OPTIONAL - set item_1
   $item_1->setNumber("1");
   $item_1->setProductNr("1");
   $item_1->setDescription("Description");
   $item_1->setPackage("");
   $item_1->setQuantity("1");
   $item_1->setItemPrice("8,55");					// MIND THE FORMAT: x.yz
   $item_1->setPrice("8,55");						// MIND THE FORMAT: x.yz
   $request->addItem($item_1);
   
   // OPTIONAL - set item_2
   #$item_2->setNumber("2");
   #$item_2->setProductNr("0123-14-14");
   #$item_2->setDescription("Jil Sander");
   #$item_2->setPackage("25 ml");
   #$item_2->setQuantity("1");
   #$item_2->setItemPrice("19.90");					// MIND THE FORMAT: x.yz
   #$item_2->setPrice("19.90");						// MIND THE FORMAT: x.yz
   #$request->addItem($item_2);
   
   // OPTIONAL - set item_n
   #$item_n->setNumber("n");
   #$item_n->setProductNr("x");
   #$item_n->setDescription("Description");
   #$item_n->setPackage("Package");
   #$item_n->setQuantity("y");
   #$item_n->setItemPrice("z.ad");					// MIND THE FORMAT: x.yz
   #$item_n->setPrice("b.ce");						// MIND THE FORMAT: x.yz
   #$request->addItem($item_n);
   
   /*
    * token generation for increased URL/Confirmation security
    * 
    * append the token with additional data available in your environment or use a better random generator
    * save the token to a database, file (preferably outside the webserver root), ... and validate the parameter "token" in mpay24_confirm.php
    */
   $ip   = (int) str_replace(".", "", $_SERVER['REMOTE_ADDR']);
   $ts   = microtime();
   $rand = mt_rand(0, $ts);
   $seed = (string) $ip * $ts * $rand;
   $token= sha1($seed);
   
   // URLs will be submitted to mPAY24 within XML stream
   $request->setSuccessURL("http://" . $_SERVER['HTTP_HOST'] . "/mpay24_templates/success.html");
   $request->setErrorURL("http://" . $_SERVER['HTTP_HOST']   . "/mpay24_templates/err.php");					// also used for directly redirecting the customer in case of an error
   $request->setConfirmURL("http://" . $_SERVER['HTTP_HOST'] . "/mpay24_templates/mpay24_confirm.php?token=$token");
   
   // create XMLFILE
   $request->createXmlFile();
   
   // DEPRECATED - use sending XML stream directly via "MDXI" parameter instead
   #$request->setGetdataURL("http://" . $_SERVER['HTTP_HOST'] . "/temp/$tid.xml");
   #$request->setXmlDir("/home/apache2/htdocs/temp");
   #$request->saveXmlFile();
   
   // TODO: set authentication information if using SoapClient. 
   // Please refer to SelectPayment.php, lines 692-703.
   $request->send();
   
   $request->parseResponse();
   
   $STATUS = $request->getSTATUS();
   $RETURNCODE = $request->getRETURNCODE();
   $LOCATION = $request->getLOCATION();
   
   if($STATUS == "OK"){
      print "STATUS: OK\r\n";
      if($RETURNCODE == "REDIRECT"){
         print "RETURNCODE: REDIRECT\r\n";
         if(!empty($LOCATION)){
            print "<p>Sie werden jetzt zu unserem Payment Service Provider (PSP) <a href='https://www.mPAY24.com'>mPAY24</a> umgeleitet. Die Zahlung wird &uuml;ber eine verschl&uuml;sselte Verbindung abgewickelt.<br/>
            Folgende URL sollte nach Klick auf folgenden Link in Ihrem Browser zu sehen sein:
            <p>Payment is conducted via a secure connection provided by our Payment Service Provider (PSP) <a href='https://www.mPAY24.com'>mPAY24</a>. The following URL should be displayed in your browser's
            location bar after clicking the following link.</p>
            <br/>
            $LOCATION</p>";
            print "<p><a href='$LOCATION'/>Weiter zur Zahlung &uuml;ber eine sichere Verbindung<br/>Continue with payment over a secure connection</a></p>";
         }
      }elseif($RETURNCODE == "OK")
         header('Location: success.html');
   }else{
      header('Location: err.php?STATUS=' . urlencode($STATUS) . '&RETURNCODE=' . urlencode($RETURNCODE));
   }
   //echo $request->getResponse();
?>